/**
 * @author sunny
 * @date 2022/10/9 10:49
 * @Description
 */
//将url参数转换成hashmap对象
function paramToHashMap(param) {
    var tmpArr = param.split("&");
    var hashMap = new Object();
    for (var i = 0; i < tmpArr.length; i++) {
        var para = tmpArr[i];
        if (para.indexOf("=") != -1) {
            var kv = para.split("=");
            hashMap[kv[0]] = kv[1];
        }
    }
    return hashMap;
}

$(function () {
    dateTiemSelect();//初始化日期格式
    $("#query").click(function () {
        if ($("#startTime").val() === "") {
            layer.msg("输入开始时间！", {icon: 2});
            $("#endTime").val("");
        }
        var startTime = new Date($("#startTime").val().replace(/\-/g, "\/"));
        var endTime = new Date($("#endTime").val().replace(/\-/g, "\/"));
        if (startTime >= endTime) {
            layer.msg("开始时间不能大于结束时间！", {icon: 2});
            $("#startTime").val("");
            $("#endTime").val("");
            return false;
        } else {
            load();
        }
    })

    load();
    upload();
})
//日期转换
Date.prototype.Format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}
//点击查询，加载数据
//输入框设置主题。
function dateTiemSelect() {
    layui.use('laydate', function () {
        var laydate = layui.laydate;
        laydate.render({
            elem: '#startTime', //新增的日期
            type: 'datetime',//日期类型date表示年月日
            /*  trigger: 'click',//如果绑定的元素非输入框，则默认事件为：click*/
            theme: '#071f3a',//自定义主题
            min: '2014-1-1',//限制最小日期
            max: '2028-12-31'//限制最大日期
        });
        laydate.render({
            elem: '#endTime', //新增的日期
            type: 'datetime',
            theme: '#071f3a',//自定义主题
        });
    });
}

//初始化加载曲线跟表格数据
function load() {
    var index = layer.load(0);

    var hdforeData = new Array();//火电预测数据
    var hdskData = new Array();//火电实况数据

    var p = {
        "startTime": $("#startTime").val(),
        "endTime": $("#endTime").val()
    };
    $.ajax({
        url: "../ShuiHuoParticularController/huoQueryResult",
        type: "get",
        contentType: "application/json",
        dataType: "json",
        data: p/*JSON.stringify(p)*/,//转换成字符串。后台使用了RequestBody注解
        success: function (data) {
            layer.close(index);
            var times = 15;
            var min = null;

            $.each(data.hdmap.ychdlist, function (index, item) {
                var time = new Date(parseInt(item.time)).Format("yyyy-MM-dd hh:mm");
                hdforeData.push({name: time, value: [time, item.value]});
            });

            $.each(data.hdmap.sjhdlist, function (index, item) {
                var time = new Date(parseInt(item.time)).Format("yyyy-MM-dd hh:mm");
                min = time.substring(14, 16);
                if (min % times == 0) {
                    hdskData.push({name: time, value: [time, item.value]});
                }
            });

            lodaTu(hdforeData, hdskData, 600);
        }
    });
}

function upload() {
    layui.use('upload', function () {
        var upload = layui.upload;
        //执行实例
        var uploadInst = upload.render({
            elem: '#btn-export', //绑定元素
            url: '../ShuiHuoParticularController/huoImport', //上传接口
            accept: 'file',//指定允许上传的文件类型，可选值有：images（图片）、file（所有文件）、video（视频）、audio（音频）
            exts: 'xls',//指定文件的后缀
            done: function (res) {
                if (res === true) {
                    layer.msg("导入成功！", {icon: 1});
                } else {
                    layer.msg("导入失败！", {icon: 2});
                }
            }
            , error: function () {
                //请求异常回调
            }
        });
    });
}

//折线图 日期，实况功率，图例，实况风速，预报功率
function lodaTu(hdforeData, hdskData, installCapacity) {
    var myChart = echarts.init(document.getElementById("main"));
    var colors = ['#6eb2ff', '#FF0000'];
    option = {
        backgroundColor: '#071f3a',//背景色
        color: colors,
        tooltip: {
            trigger: 'axis',
            axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                type: 'line'        // 默认为直线，可选为：'line' | 'shadow'
            }
        },
        legend: {
            show: true,
            //orient:'horizontal',
            //borderWidth:2,
            data: [
                {
                    name: '实况火电',
                    textStyle: {
                        fontSize: 12,
                        fontWeight: 'bolder',
                        color: '#fff'
                    },
                },
                {
                    name: '计划火电',
                    textStyle: {
                        fontSize: 12,
                        fontWeight: 'bolder',
                        color: '#fff'
                    },
                }
            ]
        },
        toolbox: {
            show: true,
            feature: {}
        },
        calculable: false,//取消拖拽
        dataZoom: {
            show: true,
            realtime: true,
            start: 0,
            end: 100
        },
        xAxis: [
            {
                //interval:0,//X轴的刻度间隔
                splitLine: {show: false},//去除网格线
                type: 'time',
                boundaryGap: true,

                //data: date,
                axisLabel: {
                    formatter: '{value}',
                    textStyle: {
                        fontWeight: "bolder",
                        color: "#fff"
                    }
                }
            }
        ],
        yAxis: [
            {
                splitLine: {show: false},//去除网格线
                name: '功率(mw)',
                // max: installCapacity,
                min: 0,
                nameTextStyle: {
                    color: '#fff'
                },
                type: 'value',
                axisLabel: {
                    formatter: '{value}'/*+'mw'*/,
                    textStyle: {
                        fontWeight: "bolder",
                        color: "#fff"
                    }
                }
            }
        ],
        series: [
            {
                name: '实况火电',
                type: 'line',
                yAxisIndex: 0,
                symbol: 'none',  //这句就是去掉点的
                data: hdskData
            },
            {
                name: '计划火电',
                type: 'line',
                yAxisIndex: 0,
                data: hdforeData,
                symbol: 'none',  //这句就是去掉点的
                itemStyle: {
                    normal: {
                        lineStyle: {
                            shadowBlur: 10,
                            shadowColor: 'rgba(0, 0, 0, 0.5)',
                            smooth: true,
                            width: 2//线宽
                        }
                    }
                }

            }
        ]
    };
    myChart.setOption(option);
}




